package com.union.core.job.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.hnjowe.util.ComUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.union.core.job.dao.impl.DailyJobDaoImpl;
import com.union.core.job.model.DailyJob;
import com.union.core.job.model.DailyJobAudi;
import com.union.core.job.service.DailyJobAudiService;
import com.union.core.organization.model.Personnel;
import com.union.core.organization.service.OrganizationParamService;
import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysService;

public class DailyJobAction extends ActionSupport {

	/**
	 * action:日常任务
	 */
	private static final long serialVersionUID = -8824737769279517759L;
	private DailyJobDaoImpl dailyJobDaoImpl;
	private SysService sysService = SysService.getInstance();
	private DailyJob job;
	private List<DailyJob> jobList;
	private int jobId;

	private Personnel per;
	//附件上传目录
	private final static String SENTIMENT_DIR = "/upload/job/";
	//日志管理
	private SysLogsService sysLogsSer;
	/*分页设置*/
	private int page = 1;		//页码
	private int intRowCount;	//总行数
	private int intPageCount;	//总页数
	private int start;			//开始
	private int intPageSize = 10;//每页显示的行数
	private int intPage = 1;	//页数变量
	/*页面对象*/
	private String startDate;
	private String endDate;
	private String queryType;
	private String queryData;
	//查询条件
	private int status;
	private int orgId;
	private int staffId;
	private String orderByName;
	//任务评阅
	private DailyJobAudiService dailyJobAudiService;
	private DailyJobAudi dailyJobAudi;
	private List<DailyJobAudi> dailyJobAudiList;
	//权限控制
	private List<String> allPower;
	//接收单位
	private List<String> orgNameList;

	private OrganizationParamService orgSer;
	public DailyJobAction(){
		dailyJobDaoImpl=new DailyJobDaoImpl();
		jobList=new ArrayList<DailyJob>();
		dailyJobAudiService=new DailyJobAudiService();
		sysLogsSer=new SysLogsService();
		orgSer=new OrganizationParamService();
	}
	//加载添加任务页面
	public String loadAddJob(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			per=sysService.getSessionListener().getPERSONNEL();
			return "addloadjob";
		}
	}
	//提交任务
	public String submitJob(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			String fileName = "";
			//上传文件
			if(job.getSfile() != null){
				fileName = ComUtil.genFileId();
				String fileext = ComUtil.getExtention(job.getSfileFileName());
				fileName = fileName + fileext;
				String fullPath = getRealPath() + SENTIMENT_DIR + fileName;
				ComUtil.copyFile(job.getSfile(), new File(fullPath));
				job.setJOB_FILE(job.getSfileFileName());
				job.setFile_path(SENTIMENT_DIR + fileName);
			}
			if(job.getReceive_list()!=null&& !"".equals(job.getReceive_list().trim())){
				job.setReceive_list(","+job.getReceive_list()+",");
			}
			boolean bl=dailyJobDaoImpl.addJob(job);
			if(bl){
				sysLogsSer.addSysLogs("提交任务【"+job.getJOB_NAME()+"】");
			}
			return "submitJob";
		}
	}
	//已经提交任务列表
	public String submited(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			staffId=sysService.getSessionListener().getPERSONNEL_ID();
			lookFor();
			return "sendedlist";
		}
	}
	//条件查询已提交任务
	public String lookByOption(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			staffId=sysService.getSessionListener().getPERSONNEL_ID();
			lookFor();
			setSession();
			return "sendedlist";
		}
	}
	//翻页
	public String turnPage(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			getSession();
			lookFor();
			return "sendedlist";
		}
	}
	//单条已提交任务
	public String sendedJob(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			job=dailyJobDaoImpl.getJobById(jobId);
			dailyJobAudiList=dailyJobAudiService.getJobAudiList(jobId);
			if(job.getReceive_list()!=null && !"".equals(job.getReceive_list())){
				orgNameList=orgSer.getOrgNameList(job.getReceive_list());
			}
			return "sendedjob";
		}
	}
	//查询列表方法
	private void lookFor(){
		if("".equals(startDate))startDate = null;
		if("".equals(endDate))endDate = null;
		if("".equals(queryData))queryData = null;
		if("".equals(queryData))queryData = null;
		if(page<1){
			intPage = 1;
		}else{
			intPage = page;
		}
		intRowCount  = dailyJobDaoImpl.getJobListCount(startDate, endDate, 
				status, orgId, staffId, queryType, queryData);
		intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
		
		if(intPage > intPageCount) intPage = intPageCount;            
		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			jobList = dailyJobDaoImpl.getJobList(startDate, endDate, status, 
					orgId, staffId, queryType, queryData, orderByName, start, intPageSize);
		}
	}
	//设置session
	private void getSession(){
		orgId = (Integer)ServletActionContext.getRequest().getSession().getAttribute("orgId");
		staffId = (Integer)ServletActionContext.getRequest().getSession().getAttribute("staffId");
		startDate = (String)ServletActionContext.getRequest().getSession().getAttribute("startDate");
		endDate = (String)ServletActionContext.getRequest().getSession().getAttribute("endDate");
		queryType = (String)ServletActionContext.getRequest().getSession().getAttribute("queryType");
		queryData = (String)ServletActionContext.getRequest().getSession().getAttribute("queryData");
		orderByName = (String)ServletActionContext.getRequest().getSession().getAttribute("orderByName");
	}

	//获取session信息
	private void setSession(){
		ServletActionContext.getRequest().getSession().setAttribute("organizationId",orgId); 
		ServletActionContext.getRequest().getSession().setAttribute("staffId",staffId); 
		ServletActionContext.getRequest().getSession().setAttribute("startDate",startDate); 
		ServletActionContext.getRequest().getSession().setAttribute("endDate",endDate); 
		ServletActionContext.getRequest().getSession().setAttribute("queryType",queryType); 
		ServletActionContext.getRequest().getSession().setAttribute("queryData",queryData); 
		ServletActionContext.getRequest().getSession().setAttribute("orderByName",orderByName);
	}
	//加载评阅页面
	public String loadAddJobAudi(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			job=dailyJobDaoImpl.getJobById(jobId);
			per=sysService.getSessionListener().getPERSONNEL();
			return "audijob";
		}
	}
	//提交任务评阅
	public String submitJobAudi(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			String fileName = "";
			//上传文件
			if(dailyJobAudi.getAfile() != null){
				fileName = ComUtil.genFileId();
				String fileext = ComUtil.getExtention(dailyJobAudi.getAfileFileName());
				fileName = fileName + fileext;
				String fullPath = getRealPath() + SENTIMENT_DIR + fileName;
				ComUtil.copyFile(dailyJobAudi.getAfile(), new File(fullPath));
				dailyJobAudi.setJob_file(dailyJobAudi.getAfileFileName());
				dailyJobAudi.setFile_path(SENTIMENT_DIR + fileName);
			}
			boolean bl=dailyJobAudiService.addJobAudi(dailyJobAudi);
			if(bl){
				sysLogsSer.addSysLogs("提交任务评阅【"+dailyJobAudi.getAudi_details()+"】");
			}
			return "receiveList";
		}
	}
	
	//接收评阅列表
	public String receiveList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			orgId=sysService.getSessionListener().getPERSONNEL().getOrganization_id();
			lookFor();
			allPower=sysService.getSessionListener().getAllPower();
			return "receiveList";
		}
	}
	//条件查询评阅任务
	public String reseiveByOption(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			orgId=sysService.getSessionListener().getPERSONNEL().getOrganization_id();
			lookFor();
			setSession();
			return "receiveList";
		}
	}
	//翻页
	public String reseivePage(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			getSession();
			lookFor();
			return "receiveList";
		}
	}
	//单条已提交任务
	public String reseivedJob(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			job=dailyJobDaoImpl.getJobById(jobId);
			dailyJobAudiList=dailyJobAudiService.getJobAudiList(jobId);
			if(job.getReceive_list()!=null && !"".equals(job.getReceive_list())){
				orgNameList=orgSer.getOrgNameList(job.getReceive_list());
			}
			return "oneaudijob";
		}
	}
	public DailyJob getJob() {
		return job;
	}
	public void setJob(DailyJob job) {
		this.job = job;
	}
	public Personnel getPer() {
		return per;
	}
	public void setPer(Personnel per) {
		this.per = per;
	}
	public String getRealPath(){
		return ServletActionContext.getServletContext().getRealPath("/");
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getIntRowCount() {
		return intRowCount;
	}
	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}
	public int getIntPageCount() {
		return intPageCount;
	}
	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getQueryType() {
		return queryType;
	}
	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}
	public String getQueryData() {
		return queryData;
	}
	public void setQueryData(String queryData) {
		this.queryData = queryData;
	}
	public List<DailyJob> getJobList() {
		return jobList;
	}
	public void setJobList(List<DailyJob> jobList) {
		this.jobList = jobList;
	}
	public int getJobId() {
		return jobId;
	}
	public void setJobId(int jobId) {
		this.jobId = jobId;
	}
	public List<DailyJobAudi> getDailyJobAudiList() {
		return dailyJobAudiList;
	}
	public void setDailyJobAudiList(List<DailyJobAudi> dailyJobAudiList) {
		this.dailyJobAudiList = dailyJobAudiList;
	}
	public List<String> getAllPower() {
		return allPower;
	}
	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}
	public DailyJobAudi getDailyJobAudi() {
		return dailyJobAudi;
	}
	public void setDailyJobAudi(DailyJobAudi dailyJobAudi) {
		this.dailyJobAudi = dailyJobAudi;
	}
	public List<String> getOrgNameList() {
		return orgNameList;
	}
	public void setOrgNameList(List<String> orgNameList) {
		this.orgNameList = orgNameList;
	}
	public String getOrderByName() {
		return orderByName;
	}
	public void setOrderByName(String orderByName) {
		this.orderByName = orderByName;
	}
}
